package me.zixing.memorandum.ui.component;

import me.zixing.memorandum.domain.DayTask;
import me.zixing.memorandum.ui.page.MainPage;
import me.zixing.memorandum.utils.AppConstant;
import me.zixing.memorandum.utils.TimingUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class RemindPanel extends JFrame implements Comparable<RemindPanel>{
    private long y;//纵坐标
    private DayTask dayTask;//提醒的任务信息

    public RemindPanel(long y, DayTask dayTask) {
        this.y = y;
        this.dayTask = dayTask;
        this.setBounds((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() - 210,(int)(10 + y),200,120);
        this.setBackground(Color.white);
        this.setUndecorated(true);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
        try{
            this.setIconImage(ImageIO.read(MainPage.class.getClassLoader().getResource("remind.png")));
        }catch (Exception e){
            e.printStackTrace();
        }
        initUI();
        initAction();
    }

    public void initUI(){
        JLabel contentLabel = new JLabel();
        contentLabel.setText(dayTask.getTitle());
        contentLabel.setFont(AppConstant.SOFT_BOLD_15);
        contentLabel.setBounds(20,12,150,30);
        JLabel timeLabel = new JLabel();
        timeLabel.setText(dayTask.getStartDateTime()+"~"+dayTask.getEndDateTime());
        timeLabel.setBounds(20,40,100,30);
        timeLabel.setFont(AppConstant.SOFT_ITALIC_15);
        this.add(contentLabel);
        this.add(timeLabel);
    }

    public void initAction(){
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                TimingUtils.removeShowedDayTasks();
            }
        });
    }

    @Override
    public int compareTo(RemindPanel o) {
        return 1;
    }
}
